﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LogicUniversityBiz
{
    public class CatalogueMaintain
    {
        public List<Catelogue> getCatalogue()
        {
            LogicUniversityEntities lue = new LogicUniversityEntities();
            var cat = lue.Catelogues
                 .AsEnumerable()
               .Select(x => x)
               .ToList();
            return cat;
        }

        public List<Catelogue> getAllbyName(string category)
        {
            using (var context = new LogicUniversityEntities())
            {
                var cat = context.ExecuteStoreQuery<Catelogue>("Select * from Catelogue where Category = '" + category + "'");
                List<Catelogue> list = new List<Catelogue>();
                foreach (Catelogue ca in cat)
                {
                    list.Add(ca);
                }
                return list;
            }
        }
        public int addCatalogue(string itemCode, string category,
            int bin ,int reorderLevel, int reorderQty, string measurement, string descrip)
        {
            using (LogicUniversityEntities lue = new LogicUniversityEntities())
            {
                Catelogue addCata = new Catelogue();
                addCata.Item_Code = itemCode;
                addCata.Category = category;
                addCata.Bin = bin; 
                addCata.Reorder_Level = reorderLevel;
                addCata.Reorder_Qty = reorderQty;
                addCata.Measurement = measurement;
                addCata.Item_Description = descrip;
                lue.AddToCatelogues(addCata);
                lue.SaveChanges();
            }
            return 0;
        }
        public int updateCatalogue(string itemCode, string category,
            int bin, int reorderLevel, int reorderQty, string measurement, string descrip)
        {
            using (LogicUniversityEntities lue = new LogicUniversityEntities())
            {
                var updateCata = lue.Catelogues.FirstOrDefault(c => c.Item_Code == itemCode);
                {
                    updateCata.Category=category;
                    updateCata.Bin=bin;
                    updateCata.Reorder_Level=reorderLevel;
                    updateCata.Reorder_Qty=reorderQty;
                    updateCata.Measurement=measurement;
                    updateCata.Item_Description=descrip;
                    lue.SaveChanges();
                }
            }
            return 0;
        }
        public void deleteCatalogue (string item_Code)
        {

            using (LogicUniversityEntities lue = new LogicUniversityEntities())
            {

                var x = lue.Catelogues.FirstOrDefault(m => m.Item_Code == item_Code);
                if (x != null)
                {
                    lue.DeleteObject(x);
                    lue.SaveChanges();
                }

            }
        }
       
    }
}